
var URL_GOBAl      = "http://118.178.187.204:8080/uplus";
var SEL_REMAIN_MSG = "--请选择--";
var projectName    = "my-ace-admin";
//var projectName    = "report";

//var URL_TABLES = "http://www.91cht.com:80/woxiang/control";
var URL_TABLES = "http://localhost:80/woxiang/control";
function getProjectURL(){
    var href = window.location.href;
    var url = href.substr(0,href.indexOf(projectName));
    url += projectName;
    return url;
}


$(document).ready(function(){
    var amcssName = "assets/css/amaran.min.css";
    var amjsName  = "assets/js/jquery.amaran.min.js";
    var amcss = $("<link rel='stylesheet' href='"+amcssName+"' />");
    var amjs = $("<script src='"+amjsName+"'></script>");
    $("body").append(amcss);
    $("body").append(amjs);
});

/**
 * http://www.amaranjs.com/
 * var inEffects = ['slideRight','slideLeft','slideBottom','slideTop'];
 * var positions = ['top left','top right','bottom right','bottom left'];
 * */
function myAlert(msg,pos){
    var position = 'top right';
    if(pos != undefined){position=pos;}

    $.amaran({
        'message'   :''+msg,
        'position'  : position,
        'inEffect'  :'slideTop',
        'outEffect' :'slideRight'
    });
}


$.fn.serializeObject = function()
{
    console.log(">serializeObject");
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function serializeObject(formId)
{
    console.log(">function serializeObject");
    var o = {};
    //var a = this.serializeArray();
    var a = $("#"+formId).serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function loginout(){
    sessionStorage.token="";
    var url = getProjectURL();
    window.location.href= url+"kindergarden/html/login.html";
}


function showLoading(){
    $("body").append($("<div id='foo'></div>"));
    var opts = {
        lines: 13 // The number of lines to draw
        , length: 22 // The length of each line
        , width: 8 // The line thickness
        , radius: 25 // The radius of the inner circle
        , scale: 1 // Scales overall size of the spinner
        , corners: 1 // Corner roundness (0..1)
        , color: '#00B0F0' // #rgb or #rrggbb or array of colors
        , opacity: 0.3 // Opacity of the lines
        , rotate: 24 // The rotation offset
        , direction: 1 // 1: clockwise, -1: counterclockwise
        , speed: 1 // Rounds per second
        , trail: 60 // Afterglow percentage
        , fps: 20 // Frames per second when using setTimeout() as a fallback for CSS
        , zIndex: 2e9 // The z-index (defaults to 2000000000)
        , className: 'spinner' // The CSS class to assign to the spinner
        , top: '38%' // Top position relative to parent
        , left: '50%' // Left position relative to parent
        , shadow: false // Whether to render a shadow
        , hwaccel: false // Whether to use hardware acceleration
        , position: 'absolute' // Element positioning
    };
    var target = document.getElementById('foo')
    var spinner = new Spinner(opts).spin(target);
}


function hideLoading(){
    $("#foo").remove();
}




function loadPostSelect(selectId){
    $.ajax({
        type:'get',
        url:URL_GOBAl+"/web/school/post/list",
        headers:{
            token:sessionStorage.token
        },
        async:false,
        data:{},
        dataType:'json',
        success:function(returnValue){
            if(returnValue != "" && returnValue !=null & returnValue !=undefined){

                var status = returnValue.status;
                var result = returnValue.result;

                if("000000" == status){
                    var obj = $("#"+selectId);
                    obj.empty();

                    var option = $("<option value=''>"+SEL_REMAIN_MSG+"</option>");
                    obj.append(option);
                    for(var i=0;i<result.length;i++){
                        var pro = result[i];
                        var name = pro.value;
                        var id = pro.key;

                        var option = $("<option pid='"+id+"' value='"+name+"'>"+name+"</option>");
                        obj.append(option);
                    }

                }else{
                    myAlert("请求失败，请检测网络后重新加载！");
                }
            }else{
                myAlert("请求返回值为空，请检测用户权限！");
            }
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
        }
    });
}


function loadTeacherSelect(selectId){
    $.ajax({
        type:'get',
        url:URL_GOBAl+"/web/school/list/teacher",
        headers:{
            token:sessionStorage.token
        },
        async:false,
        data:{type:'3'},  // 所有老师
        dataType:'json',
        success:function(returnValue){
            if(returnValue != "" && returnValue !=null & returnValue !=undefined){

                var status = returnValue.status;
                var result = returnValue.result;

                if("000000" == status){
                    var obj = $("#"+selectId);
                    obj.empty();

                    var option = $("<option value=''>"+SEL_REMAIN_MSG+"</option>");
                    obj.append(option);
                    for(var i=0;i<result.length;i++){
                        var pro = result[i];
                        var name = pro.realname;
                        var id = pro.aid;

                        var option = $("<option pid='"+id+"' value='"+id+"'>"+name+"</option>");
                        obj.append(option);
                    }

                }else{
                    myAlert("请求失败，请检测网络后重新加载！");
                }
            }else{
                myAlert("请求返回值为空，请检测用户权限！");
            }
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
        }
    });
}


function loadClassSelect(selectId){
    $.ajax({
        type:'get',
        url:URL_GOBAl+"/web/school/class/list",
        headers:{
            token:sessionStorage.token
        },
        async:false,
        data:{},
        dataType:'json',
        success:function(returnValue){
            if(returnValue != "" && returnValue !=null & returnValue !=undefined){

                var status = returnValue.status;
                var result = returnValue.result;

                if("000000" == status){
                    var obj = $("#"+selectId);
                    obj.empty();

                    var option = $("<option value=''>"+SEL_REMAIN_MSG+"</option>");
                    obj.append(option);
                    for(var i=0;i<result.length;i++){
                        var pro = result[i];
                        var name = pro.name;
                        var id = pro.cid;

                        var option = $("<option pid='"+id+"' value='"+id+"'>"+name+"</option>");
                        obj.append(option);
                    }

                }else{
                    myAlert("请求失败，请检测网络后重新加载！");
                }
            }else{
                myAlert("请求返回值为空，请检测用户权限！");
            }
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
        }
    });
}